package com.zeedev.flyingsaucer.data;

import android.text.TextUtils;
import com.zeedev.flyingsaucer.data.Command.Operator;

import java.io.Serializable;
import java.util.*;

public class ComplexCommand implements SQLFormattable, Serializable {

	String property;
	Set<String> propertyValues;
	Map<String, String> columns;
	
	public ComplexCommand(String property, Set<String> propertyValues) {
		this.property = property;
		this.propertyValues = propertyValues;
		columns = new HashMap<String, String>();
		columns.put(property, property);
	}
	
	@Override
	public String getFormattedSQL() {
		List<Command> templist = new ArrayList<Command>(propertyValues.size());
		for (String cur : propertyValues) {
			if (!TextUtils.isEmpty(cur)) {//cur != null
				Command cmd = new Command(property, cur, Operator.EQUAL );
				templist.add(cmd);
			}
		}
		return getOrSql(templist);
	}

	private static final String getOrSql(List<Command> templist) {
		StringBuilder selectionWhere = new StringBuilder();
		//if (templist != null && !templist.isEmpty()) {
			selectionWhere.append("( ");
			for (int i = 0; i < templist.size(); i++) {
				Command cmd = templist.get(i);
				selectionWhere.append(cmd.getFormattedSQL());
				if (i != templist.size()-1) {
					selectionWhere.append(" OR ");
				}
			}
			selectionWhere.append(" )");
		//}
		return selectionWhere.toString();
	}

	@Override
	public Boolean isComplete() {
		Boolean ret = true;
		if (TextUtils.isEmpty(this.property))
			ret = false;
		if (propertyValues.isEmpty())
			ret = false;
		return ret;
	}

	@Override
	public Map<String, String> getColumns() {
		return columns;
	}
	
	
}
